package br.com.skytef.servlet.POS;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.skytef.beans.POS.LogUser;
import br.com.skytef.beans.POS.Logica;
import br.com.skytef.beans.POS.SystemSession;
import br.com.skytef.dao.OrderDao;
import br.com.skytef.entity.MessageEntity;
import br.com.skytef.entity.OrderItem;
import br.com.skytef.entity.OrderVMachineEntity;
import br.com.skytef.entity.ProductMerchandiseEntity;
import br.com.skytef.entity.UserEntity;

public class ServletItemAdd implements Logica {
	private SystemSession sysSessionRecover = null;
	private SystemSession sysSessionNew = null;
	private MessageEntity msg = new MessageEntity();

	@Override
	public String executa(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		
		sysSessionRecover = null;
		sysSessionNew = null;
		msg = new MessageEntity();

		try {
			sysSessionRecover = (SystemSession) session.getAttribute("SysSession");
			sysSessionNew = sysSessionRecover;

			sysSessionRecover.getProductItem().getDescrMerchandise();
			
			double quantity;

			quantity = Double.parseDouble(request.getParameter("QtdeItem"));
						
			if(sysSessionRecover.getActionMain().equals("itemAdd")){
				OrderItem item = new OrderItem();
				OrderDao orderDao = new OrderDao();
				ProductMerchandiseEntity product;
				OrderVMachineEntity order;				
				LogUser login;
				UserEntity user;

				product = sysSessionRecover.getProductItem();
				order = sysSessionRecover.getOrder();
				login = sysSessionRecover.getLogin();
				user = login.getUser();
				
				item.setOrderId(order.getOrderId());
				item.setAction(sysSessionRecover.getActionMain());
				item.setProductId(product.getProductId());
				item.setUserId(user.getUserId());
				item.setPosId(login.getPos().getPosId());
				item.setHour(null);
				item.setUnitValue(0);
				item.setQuantity(quantity);
				item.setObs("");		
				
				order.setOrderItens(item);	
				session.setAttribute("MenuSelProduct","");

				msg.setTitle("Item Adicionado");
				msg.setMessage("Item: " + sysSessionRecover.getProductItem().getDescrMerchandise());
				
				sysSessionNew.setMessage(msg);
				sysSessionNew.setProductItem(null);
				sysSessionNew.setActionMain("orderReturMenu");
				sysSessionNew.setNextServlet("ServletOrderSelect");
				sysSessionNew.setDisplayTitle("Item Adicionado");
				sysSessionNew.setNextView("/jsp/POS/ModMensagem.jsp");
				
				sysSessionNew.setOrder(order);
				session.setAttribute("SysSession",sysSessionNew);
				
				//this.printOrder();
				
			}else{
					
				
			}
			

		} catch (Exception e) {
			System.out.println("##> Erro ao processar a servlet: " + sysSessionNew.getNextServlet() + "\n" + e.getMessage());
		}
		return sysSessionNew.getNextView();
	}
	
	private void printOrder(){
		try {
			OrderVMachineEntity order;
			order = sysSessionNew.getOrder();
			
			
			System.out.println("ID     : " + order.getOrderId());
			System.out.println("IDPOS  : " + order.getPosId());
			System.out.println("Usuario: " + order.getUserId());
			System.out.println("Itens");
			for (int i = 0; i < order.getOrderItens().size();i++){
				System.out.print("Cod " + order.getOrderItens().get(i).getProductId());
				System.out.print(" - Qtd " + order.getOrderItens().get(i).getQuantity());
				System.out.print(" - Pos " + order.getOrderItens().get(i).getPosId());
				System.out.println("");
			}
			
			System.out.println("Menus: " + sysSessionNew.getAllMenu().size());
			for (int i = 0; i < sysSessionNew.getAllMenu().size();i++){
				System.out.println("Cod " + sysSessionNew.getMenu(i).get(0).getDescription());
			}
		} catch (Exception e) {
			System.out.println("##> Erro ao processar a servlet: " + sysSessionNew.getNextServlet() + "\n" + e.getMessage());
		}
	}
	
	private void returnMenu(){
		
	}

}
